<?php
namespace App\Controller;
use Cake\Collection\Collection;

class UserAcosController extends AppController{

    public function initialize()
    {
        parent::initialize();
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：获取指定的权限
     * */
    public function getView()
    {
        $id  = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $user_acos = $this->UserAcos->findById($id)->first();
        if(empty($user_acos)){
            $this->G->error("user_aco_error");
            return null;
        }
        $user_acos['memo'] = json_decode($user_acos['memo'], true);
        $this->G->success('Success', $user_acos);
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：获取列表
     * */
    public function getList()
    {
        $user_acos = $this->UserAcos->find()
            ->select(['id','alias','parent_id','status'])
            ->order(['lft asc'])
            ->toArray();
        if(!empty($user_acos)){
            $collection = new Collection($user_acos);
            $user_acos = $collection->nest('id', 'parent_id')->toList();
        }
        $this->G->success('Success', $user_acos);
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：删除权限
     * */
    public function delete()
    {
        $id  = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $user_acos = $this->UserAcos->findById($id)->select(['id'])->first();
        if(empty($user_acos)){
            $this->G->error("user_aco_error");
            return null;
        }
        $this->UserAcos->deleteAll(['id'=>$id]);
        $this->loadComponent('UserAroCache');
        $this->UserAroCache->createAllCache();
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：修改权限
     * */
    public function update()
    {
        $id = $this->request->getData("id");
        $parent_id = $this->request->getData("parent_id");
        $alias = $this->request->getData("alias");
        $memo = $this->request->getData("memo");
        $status = $this->request->getData("status");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(empty(trim($alias))){
            $this->G->error("alias_error");
            return null;
        }
        if(empty($memo)){
            $this->G->error("memo_error");
            return null;
        }
        if(!in_array($status, ['enabled','disabled','hidden'])){
            $this->G->error("status_error");
            return null;
        }
        $parent_id = empty($parent_id) ? 0 : $parent_id;
        $user_acos = $this->UserAcos->findById($id)->select(['id'])->first();
        if(empty($user_acos)){
            $this->G->error("user_aco_error");
            return null;
        }
        $user_aco = $this->UserAcos->newEntity();
        $user_aco->id = $id;
        $user_aco->parent_id = $parent_id;
        $user_aco->alias = $alias;
        $user_aco->memo = json_encode($memo);
        $user_aco->status = $status;
        $this->UserAcos->save($user_aco);
        $this->loadComponent('UserAroCache');
        $this->UserAroCache->createAllCache();
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：新增权限
     * */
    public function add()
    {
        $parent_id = $this->request->getData("parent_id");
        $alias = $this->request->getData("alias");
        $memo = $this->request->getData("memo");
        $status = $this->request->getData("status");
        $parent_id = empty($parent_id) ? 0 : $parent_id;
        if(empty(trim($alias))){
            $this->G->error("alias_error");
            return null;
        }
        if(empty($memo)){
            $this->G->error("memo_error");
            return null;
        }
        if(!in_array($status, ['enabled','disabled','hidden'])){
            $this->G->error("status_error");
            return null;
        }
        $user_aco = $this->UserAcos->newEntity();
        $user_aco->parent_id = $parent_id;
        $user_aco->alias = $alias;
        $user_aco->memo = json_encode($memo);
        $user_aco->status = $status;
        $this->UserAcos->save($user_aco);
        $this->loadComponent('UserAroCache');
        $this->UserAroCache->createAllCache();
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：获取权限列表
     * */
    public function index()
    {
        $acos = $this->UserAcos->find()
            ->where(["status !=" => "disabled"])
            ->select(["id","parent_id","alias","icon","link"])
            ->order(['lft' => 'asc'])
            ->toArray();
        $this->G->success("Success",$acos);
    }
}
?>